package zhieasy.cn.xpu_helper.controller.forum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.forum.ForumFavoriteUser;
import zhieasy.cn.model.forum.ForumInfo;
import zhieasy.cn.model.forum.ForumLikeUser;
import zhieasy.cn.service.forum.*;

import java.util.HashMap;

/**
 * @author DaHang
 * @date 2020/8/29 16:40
 */
@RestController
@RequestMapping("/forum")
@Api(tags = "论坛模块，帖子接口")
public class ForumController {

    @Autowired
    ForumInfoService forumInfoService;
    @Autowired
    ForumCommentRootService forumCommentRootService;
    @Autowired
    ForumCommentRelyService forumCommentRelyService;
    @Autowired
    ForumLikeUserService forumLikeUserService;
    @Autowired
    ForumFavoriteUserService forumFavoriteUserService;

    SecurityContext context = SecurityContextHolder.getContext();

    @ApiOperation(value = "获取所有帖子接口",tags = "无参，按照时间顺序")
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResultVO getAllForums(@ApiParam("页数")@RequestParam("pageIndex")int pageIndex,
                                 @ApiParam("每页要多少数据")@RequestParam("pageSize")int pageSize){
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        return forumInfoService.getAllForums(pageIndex,pageSize,openId);
    }

    @RequestMapping(value = "/{forumId}",method = RequestMethod.GET)
    @ApiOperation(value = "获取某个帖子的详细内容",tags = "获取某个帖子的详细内容")
    public ResultVO getForumsDetail(@ApiParam("帖子的Id")@PathVariable("forumId") int forumId){
        ForumInfo forumDetail = forumInfoService.getById(forumId);
        return ResultUtils.success(forumDetail);
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    @ApiOperation(value = "修改帖子中的内容",tags = "修改帖子中的内容")
    public ResultVO updateForum(@RequestBody ForumInfo forumInfo){
        boolean b = forumInfoService.updateById(forumInfo);
        if (b){
            return ResultUtils.success();
        }
        return ResultUtils.error("修改失败");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加帖子",tags = "增加帖子")
    public ResultVO addForum(@ApiParam("帖子类")@RequestBody ForumInfo forumInfo){
        forumInfo.setOpenId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        forumInfo.setType("普通");
        forumInfo.setLikes(0L);
        forumInfo.setClicks(0L);
        forumInfo.setForward(0L);
        forumInfo.setValid(1);
        forumInfo.setCreateTime(System.currentTimeMillis());
        forumInfo.setUpdateTime(System.currentTimeMillis());
        boolean b = forumInfoService.save(forumInfo);
        if (b){
            return ResultUtils.success();
        }
        return ResultUtils.error("增加失败");
    }
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO insertForum(@RequestBody  ForumInfo info){
        info.setOpenId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        info.setType("普通");
        info.setLikes(0L);
        info.setClicks(0L);
        info.setForward(0L);
        info.setValid(1);
        info.setCreateTime(System.currentTimeMillis());
        info.setUpdateTime(System.currentTimeMillis());
        boolean b = forumInfoService.save(info);
        if (b){
            return ResultUtils.success();
        }
        return ResultUtils.error("增加失败");
    }

    @RequestMapping(value = "/{forumId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除帖子",notes = "删除帖子")
    public ResultVO deleteForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId){
        boolean b = forumInfoService.removeById(forumId);
        if (b) {
            return ResultUtils.success();
        }
        return ResultUtils.error("删除失败");
    }

    @RequestMapping(value = "/like/{forumId}",method = {RequestMethod.GET,RequestMethod.PATCH})
    @ApiOperation(value = "点赞",notes = "点赞后点赞数+1")
    public ResultVO likeForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId){
        //获取一下个人信息
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        //给这个人加到数据库里面去
        boolean save = forumLikeUserService.save(new ForumLikeUser(forumId, openId));

        if (!save) {
            return ResultUtils.error("添加失败");
        }
        //还得让forumInfo里面的like+1
        forumInfoService.addLike(forumId);
        return ResultUtils.success(forumLikeUserService.count());
    }

    @RequestMapping(value = "/disLike/{forumId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "取消点赞",notes = "取消点赞后点赞数-1")
    public ResultVO disLikeForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId){
        //获取一下个人信息
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        //给这个人加到数据库里面去
        boolean save = forumLikeUserService.removeByMap(new HashMap<String, Object>(){{
            put("id",forumId);
            put("openId",openId);
        }});
        if (!save) {
            return ResultUtils.error("取消失败");
        }
        forumInfoService.subtractLikeOfForum(forumId);
        return ResultUtils.success(forumLikeUserService.count());
    }


    @RequestMapping(value = "/forward/{forumId}",method = {RequestMethod.GET,RequestMethod.PATCH})
    @ApiOperation(value = "分享",notes = "分享数+1")
    public ResultVO forwardForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId){
        return forumInfoService.forwardForum(forumId);
    }

    @RequestMapping(value = "/favorite/{forumId}",method = {RequestMethod.POST})
    @ApiOperation(value = "收藏帖子",notes = "收藏帖子")
    public ResultVO favoriteForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId){
        boolean save = forumFavoriteUserService.save(new ForumFavoriteUser(forumId, WechatUser.getWechatUserOpenIdFromSecurityContext()));
        if (save){
            return ResultUtils.success("收藏成功");
        }
        return ResultUtils.error("收藏失败");
    }

    @RequestMapping(value = "/favorite/{forumId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "取消收藏帖子",notes = "取消收藏帖子+1")
    public ResultVO cancelFavoriteForum(@ApiParam("帖子Id")@PathVariable("forumId")int forumId) {
        boolean b = forumFavoriteUserService.removeByMap(new HashMap<String, Object>() {{
            put("id", forumId);
            put("openId", WechatUser.getWechatUserOpenIdFromSecurityContext());
        }});
        if (b) {
            return ResultUtils.success("取消收藏成功");
        }
        return ResultUtils.error("取消收藏失败");
    }












}
